package com.qfedu.yanglao20admin.plancase.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.enuminfo.StateEnum;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.plancase.CaseAffiliation;
import com.qfedu.yanglao20admin.plancase.dao.CaseAffiliationDao;
import com.qfedu.yanglao20admin.plancase.param.CaseAffiliationParam;
import com.qfedu.yanglao20admin.plancase.service.CaseAffiliationService;
import com.qfedu.yanglao20admin.plancase.vo.CaseAffiliationVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2023/4/23 12:48
 */
@Service
public class CaseAffiliationServiceImpl implements CaseAffiliationService {

    @Resource
    private CaseAffiliationDao caseAffiliationDao;

    @Override
    public Page<CaseAffiliationVO> pageCaseAffiliation(CaseAffiliationParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<CaseAffiliationVO> page = new Page<>(param.getPageNum(), param.getPageSize());

        List<CaseAffiliationVO> caseAffiliationVOS = caseAffiliationDao.selectList(param);

        //  循环遍历
        for (CaseAffiliationVO vo :
                caseAffiliationVOS) {
            StateEnum stateById = StateEnum.getStateById(vo.getEnableFlag());
            vo.setStateDesc(stateById.getDesc());
        }

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = caseAffiliationDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(caseAffiliationVOS);

        return page;
    }

    @Override
    public void deleteById(Integer id) {
        caseAffiliationDao.deleteById(id);
    }

    @Override
    public CaseAffiliation selectById(Integer id) {
        return caseAffiliationDao.selectById(id);
    }

    @Override
    public void insertCaseAffiliation(CaseAffiliation caseAffiliation) {

        if (caseAffiliation.getAffiliation() == null || caseAffiliation.getAffiliation().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写预案归属");
        }

        CaseAffiliation byaffiliation = caseAffiliationDao.selectByaffiliation(caseAffiliation.getAffiliation());
        if (byaffiliation != null) {
            throw new CommonException(CommonConstants.ERROR, "该预案归属已存在");
        }

        caseAffiliationDao.insert(caseAffiliation);
    }

    @Override
    public void updateCaseAffiliation(CaseAffiliation caseAffiliation) {
        if (caseAffiliation.getAffiliation() == null || caseAffiliation.getAffiliation().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写预案归属");
        }

        CaseAffiliation byaffiliation = caseAffiliationDao.selectByaffiliation(caseAffiliation.getAffiliation());
        if (byaffiliation != null) {
            if (!byaffiliation.getId().equals(caseAffiliation.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该预案归属已存在");
            }
        }

        caseAffiliationDao.updateById(caseAffiliation);
    }

    @Override
    public List<CaseAffiliation> selectAll() {
        return caseAffiliationDao.selectAll();
    }
}
